<?php

class Creative extends Model_Abstract
{
	public function getCreatives()
	{
		try
		{
			$sql = 'select * from creatives';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getListCreatives(){
		try
		{
			$sql = 'select id, name, html, url from creatives';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getCreativeById($id)
	{
		try
		{
			$sql = 'select * from creatives where id=?';
			$result = $this->_connection->query($sql, array($id));
			if($result)
				return $result[0];
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	
	
	public function add($name, $type, $size, $url, $html)
	{
		try
		{
			$sql = 'insert into creatives set  name=?, type=?, size=?, url=?, html=?';
			$result = $this->_connection->execute($sql, array( $name, $type, $size, $url, htmlentities($html)));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function update($name, $type, $size, $url, $html, $id)
	{
		try
		{
			$sql = 'update creatives set  name=?, type=?, size=?, url=?, html=? where id=?';
			$result = $this->_connection->execute($sql, array( $name, $type, $size, $url, htmlentities($html), $id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function delete($id){
		try
		{
			$sql = 'delete from creatives where id=?';
			$result = $this->_connection->execute($sql, array($id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>